package com.loong.musicdemo.ui.cloudtown;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.widget.LinearLayout;

import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.loong.musicdemo.R;
import com.loong.musicdemo.ui.cloudtown.videos.SectionsPagerAdapter;
import com.loong.musicdemo.ui.video.VideoFragment;
import com.loong.musicdemo.util.dataDeal.PlayListDeal;

import java.util.ArrayList;
import java.util.List;

public class CloudTownFragment extends Fragment {
    private ViewPager viewPager;//滑动页
    private TabLayout tabs;//导航
    private LinearLayout tips;//小提示


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_cloud_town, container, false);
        viewPager = root.findViewById(R.id.view_pager);
        tabs = root.findViewById(R.id.tabs);
        tips = root.findViewById(R.id.tips);
        new GetVideoGroupList().execute();
        return root;
    }

    @SuppressLint("StaticFieldLeak")
    class GetVideoGroupList extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected List<String> doInBackground(Void... voids) {
            return PlayListDeal.getPlayHotList();
        }

        @Override
        protected void onPostExecute(List<String> list) {
            super.onPostExecute(list);
            if (list == null || list.size() == 0) {
                //数据未获取到
                tips.setVisibility(View.VISIBLE);
                viewPager.setVisibility(View.GONE);
                tabs.setVisibility(View.GONE);
            } else {
                //热门歌单分类信息
                if (isAdded()) {
                    SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(), list);
                    viewPager.setAdapter(sectionsPagerAdapter);
                    tabs.setupWithViewPager(viewPager);
                }
                viewPager.setVisibility(View.VISIBLE);
                tabs.setVisibility(View.VISIBLE);
            }
        }
    }
}
